package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
@Api(tags = "购物车相关接口")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 往购物车中添加数据
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);
        //获取当前用户的id
        Long currentId = BaseContext.getCurrentId();
        //设置用户id，指定当前是哪个用户的购物车数据
        //将购物车的userId设置为当前用户的id
        shoppingCart.setUserId(currentId);
        //获取购物车中的菜品 id
        Long dishId = shoppingCart.getDishId();
        //购物车的筛选条件:
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        //1.用户id一致
        shoppingCartQueryWrapper.lambda().eq(ShoppingCart::getUserId, currentId);
        //2.添加的是菜品还是套餐
        if (dishId != null) {
            //添加的是菜品
            shoppingCartQueryWrapper.lambda().eq(ShoppingCart::getDishId, dishId);
        } else {
            //否则添加的是套餐
            shoppingCartQueryWrapper.lambda().eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //3.如果购物车中存在相同的东西,那么就在当前的基础上数量加1,否则直接设为1
        //3.1获取购物车中存入的数据
        ShoppingCart cartServiceOne = shoppingCartService.getOne(shoppingCartQueryWrapper);

        //3.2判断
        if (cartServiceOne != null) {
            //已经存在
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //不存在
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车...");
        Long currentId = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.lambda().eq(ShoppingCart::getUserId, currentId);
        shoppingCartQueryWrapper.lambda().orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(shoppingCartQueryWrapper);

        return R.success(list);
    }


    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.lambda().eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        shoppingCartService.remove(shoppingCartQueryWrapper);
        return R.success("清空购物车成功");
    }


    //减少货物数量
    //按照前端传来的id判断是dishid还是setmealid
    //判断购物车中这个东西的数量是不是为0 为0则返回减少失败
//    如果不是0则将购物车中货物的数量number获取 设置为number减一 update 返回修改成功

    /**
     * 删除一个
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {//当前只有dishid 或者 setmealid
        Long currentId = BaseContext.getCurrentId();//当前用户的id
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);
        if (dishId != null) {
            //说明是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
//            说明是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);
        log.info("获取的购物车信息:" + one.toString());
        Integer number = one.getNumber();//得到当前的数量

        if (number > 1) {
            one.setNumber(number - 1);
            shoppingCartService.updateById(one);
        } else {
            one.setNumber(number - 1);
            shoppingCartService.updateById(one);
            //不能直接删除,因为number还存着1
            shoppingCartService.removeById(one);
            log.info("获取的删除后的购物车信息:" + one.toString());
        }
        return R.success(one);
    }
}
